<template>
    <div>
        <el-form ref="form" :model="projectForm" label-width="80px" size="small">
            <el-form-item label="项目标题">
                <el-input v-model="projectForm.title"></el-input>
            </el-form-item>
            <el-form-item label="项目地址">
                <el-input v-model="projectForm.url"></el-input>
            </el-form-item>
            <el-form-item label="github地址">
                <el-input v-model="projectForm.github_url"></el-input>
            </el-form-item>
            <el-form-item label="封面图片">
                <el-upload
                    class="avatar-uploader"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="projectForm.pic" :src="projectForm.pic" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="项目简介">
                <el-input type="textarea" v-model="projectForm.desc"></el-input>
            </el-form-item>
            <el-form-item label="是否发布">
                <el-radio-group v-model="projectForm.is_show" size="mini">
                    <el-radio  label="显示"></el-radio>
                    <el-radio  label="不显示"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item size="large">
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>

export default {
    name: 'projectEdit',
     data() {
      return {
        projectForm: {
          title: '',
          pic: '',
          desc: '',
          github_url: '',
          url: '',
          is_show: 1,
        },
      };
    },
    methods: {
     onSubmit() {
        console.log('submit!');
      },
      handleAvatarSuccess(res, file) {
        this.projectForm.pic = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
 
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 300px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 300px;
    height: 178px;
    display: block;
  }
.el-input {
  width: 400px;
}
.el-textarea {
  width: 400px;
}
</style>